<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">

    </avue-crud>
  </basic-container>
</template>

<script>
  import {add, getDetail, getList, remove, update} from "@/api/app/coal";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: false,
          viewBtn: true,
          addBtn: false,
          editBtn: false,
          delBtn: false,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "订单编号",
              prop: "id",
              width: 150
            },
            {
              label: "商家姓名",
              prop: "fwname",
            },
            {
              label: "商家下单电话",
              prop: "fwphone",
              width: 110
            },
            {
              label: "服务地址",
              prop: "fwaddress",
              // search: true,
              width: 150,
              hide: true,
              rules: [{
                required: true,
                message: "请输入客户地址 商家地址",
                trigger: "blur"
              }]
            },
            {
              label: "服务地址纬度",
              prop: "fwjwd",
              hide: true,
              rules: [{
                required: true,
                message: "服务地址纬度",
                trigger: "blur"
              }]
            },
            {
              label: "服务详细地址",
              prop: "fwxxdz",
              width: 160,
              rules: [{
                required: true,
                message: "请输入服务详细地址",
                trigger: "blur"
              }]
            },
            {
              label: "订单金额",
              prop: "orderprice",
            },

            {
              label: "优惠券编号",
              prop: "discountid",
              hide: true,
            },
            {
              label: "优惠券",
              prop: "discountprice",
            },

            {
              label: "实际支付",
              prop: "payamount",
              rules: [{
                required: true,
                message: "请输入订单实际支付金额",
                trigger: "blur"
              }]
            },
            {
              label: '拍照参考',
              prop: 'takephoto',
              dataType: 'string',
              type: 'img'
            },
            {
              label: "预支付ID",
              prop: "topayid",
              hide: true,
              rules: [{
                required: true,
                message: "请输入预支付ID",
                trigger: "blur"
              }]
            },

            {
              label: "支付时间",
              prop: "paytime",
              rules: [{
                required: true,
                message: "请输入支付时间",
                trigger: "blur"
              }]
            },
            {
              label: "交易流水号  全局唯一",
              prop: "tradeno",
              hide: true,
              rules: [{
                required: true,
                message: "请输入交易流水号  全局唯一",
                trigger: "blur"
              }]
            },
            {
              label: "下单商家ID",
              prop: "cuserid",
              hide: true,
              rules: [{
                required: true,
                message: "请输入下单商家ID",
                trigger: "blur"
              }]
            },
            {
              label: "商家名称",
              prop: "cusername",
              search: true,
              rules: [{
                required: true,
                message: "请输入下单商家名称",
                trigger: "blur"
              }]
            },
            {
              label: "商家电话",
              prop: "cuserphone",
              search: true,
              width: 120,
              rules: [{
                required: true,
                message: "请输入下单商家电话",
                trigger: "blur"
              }]
            },
            {
              label: "下单备注",
              prop: "ordermark",
              rules: [{
                required: true,
                message: "请输入下单备注",
                trigger: "blur"
              }]
            },
            {
              label: "下单时间",
              prop: "xdtime",
              width: 140,
              rules: [{
                required: true,
                message: "请输入下单时间",
                trigger: "blur"
              }]
            },
            {
              label: "汽配城ID",
              prop: "cuserqpcid",
              hide: true,
              rules: [{
                required: true,
                message: "请输入汽配城ID",
                trigger: "blur"
              }]
            },
            {
              label: "汽配城名称",
              prop: "cuserqpname",
              width: 140,
              rules: [{
                required: true,
                message: "请输入汽配城名称",
                trigger: "blur"
              }]
            },
            {
              label: "1:待接单 2:进行中 3:待支付 4:已完成 5:已取消",
              prop: "orderstatus",
              rules: [{
                required: true,
                message: "请输入1:待接单 2:进行中 3:待支付 4:已完成 5:已取消",
                trigger: "blur"
              }]
            },
            {
              label: "接单人ID_工人",
              prop: "workerid",
              hide:
                true,
              rules: [{
                required: true,
                message: "请输入接单人ID_工人",
                trigger: "blur"
              }]
            },
            {
              label: "工人姓名",
              prop: "workername",
              search: true,
              rules: [{
                required: true,
                message: "请输入接单人姓名_工人",
                trigger: "blur"
              }]
            },
            {
              label: "工人电话",
              prop: "workerphone",
              search: true,
              rules: [{
                required: true,
                message: "请输入接单工人电话",
                trigger: "blur"
              }]
            },
            {
              label: "接单时间",
              prop: "workerjdtime",
              width: 140,
              rules: [{
                required: true,
                message: "请输入接单时间",
                trigger: "blur"
              }]
            },
            // {
            //   label: "工人头像",
            //   prop: "workeravatar",
            //   rules: [{
            //     required: true,
            //     message: "请输入工人头像",
            //     trigger: "blur"
            //   }]
            // },
            {
              label: '工人头像',
              prop:
                'workeravatar',
              type:
                'upload',
              loadText:
                '附件上传中，请稍等',
              listType:
                "picture-img",
              span:
                24,
              propsHttp:
                {
                  res: 'data'
                }
              ,
              action: "/api/blade-app/filemgr/upload"
            },
            {
              label: "满意度",
              prop: "workermyd",
              rules: [{
                required: true,
                message: "请输入满意度",
                trigger: "blur"
              }]
            },
            {
              label: "取消人ID",
              prop: "qxuserid",
              hide:
                true,
              rules: [{
                required: true,
                message: "请输入取消人ID",
                trigger: "blur"
              }]
            },
            {
              label: "取消人",
              prop: "qxname",
              search: true,
              rules: [{
                required: true,
                message: "请输入取消人姓名",
                trigger: "blur"
              }]
            },
            {
              label: "违约金额",
              prop: "wymoney",
              rules: [{
                required: true,
                message: "请输入违约金额_取消人支付",
                trigger: "blur"
              }]
            },
            {
              label: "1:正常单 2:已违约 3:已处理",
              prop: "qxstatus",
              rules: [{
                required: true,
                message: "请输入1:正常 2:已违约 3:已处理",
                trigger: "blur"
              }]
            },
            {
              label: "删除状态 1:正常 2:已删除",
              prop: "delflag",
              hide:
                true,
              rules: [{
                required: true,
                message: "请输入删除状态 1:正常 2:已删除",
                trigger: "blur"
              }]
            },
            {
              label: "创建时间",
              prop: "ctime",
              width: 140,
              rules: [{
                required: true,
                message: "请输入创建时间",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.coal_add, false),
          viewBtn: this.vaildData(this.permission.coal_view, false),
          delBtn: this.vaildData(this.permission.coal_delete, false),
          editBtn: this.vaildData(this.permission.coal_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
